<?php
/**
 * aheadWorks Co.
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the EULA
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://ecommerce.aheadworks.com/AW-LICENSE-COMMUNITY.txt
 * 
 * =================================================================
 *                 MAGENTO EDITION USAGE NOTICE
 * =================================================================
 * This package designed for Magento COMMUNITY edition
 * aheadWorks does not guarantee correct work of this extension
 * on any other Magento edition except Magento COMMUNITY edition.
 * aheadWorks does not provide extension support in case of
 * incorrect edition usage.
 * =================================================================
 *
 * @category   AW
 * @package    AW_Vidtest
 * @copyright  Copyright (c) 2010-2011 aheadWorks Co. (http://www.aheadworks.com)
 * @license    http://ecommerce.aheadworks.com/AW-LICENSE-COMMUNITY.txt
 */
class AW_Vidtest_Admin_AuthsubController extends Mage_Adminhtml_Controller_Action
{
    public function authAction()
    {
        if ($apiModelCode = $this->getRequest()->getParam('api_model_code')){
            $apiModel = Mage::getSingleton('vidtest/connector')->getApiModel($apiModelCode);
            $requestedResource = 'http://gdata.youtube.com';
            $returnUri = $this->getUrl('vidtest_admin/admin_authsub/return');       
        }
        $this->getResponse()->setRedirect(Zend_Gdata_AuthSub::getAuthSubTokenUri($returnUri, $requestedResource, 0, 1));
    }

    public function returnAction()
    {
        if ($token = $this->getRequest()->getParam('token')){
            $session_token = Zend_Gdata_AuthSub::getAuthSubSessionToken($token);
            Mage::getConfig()->saveConfig('vidtest/youtube/access_token', $session_token);
        }
        $this->_redirect('adminhtml/system_config/edit', array('section'=>'vidtest'));
    }

    public function revokeAction()
    {
        if ($apiModelCode = $this->getRequest()->getParam('api_model_code')){
            if (($connector = Mage::getSingleton('vidtest/connector'))
                        && ($apiModel = $connector->getApiModel($apiModelCode))
                        && $apiModel->logOut())
            {
                Mage::getConfig()->saveConfig('vidtest/youtube/access_token', '');
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('vidtest')->__('Token revoked successfull'));
            } else {
                Mage::getSingleton('adminhtml/session')->addError(Mage::helper('vidtest')->__('Token revoking was failed'));
            }
        }                
        $this->_redirect('adminhtml/system_config/edit', array('section'=>'vidtest'));
    }
}